package com.example.mountainwalker.controller;

import com.example.mountainwalker.entity.ScenicCommentInfo;
import com.example.mountainwalker.service.ScenicCommentService;
import com.example.mountainwalker.service.impl.PageRequest;
import com.example.mountainwalker.service.impl.PageResult;
import com.example.mountainwalker.vo.ViewScenicComment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.View;
import java.util.List;

@RestController
@RequestMapping("/sc")
@CrossOrigin
public class ScenicCommentController {

    @Autowired
    private ScenicCommentService scenicCommentService;

    @GetMapping("/list")
    public List<ScenicCommentInfo> getScenicCommentList() {
        return scenicCommentService.getScenicCommentInfo();
    }

    @GetMapping("/find")
    public ScenicCommentInfo getScenicCommentById(Integer id) {
        return scenicCommentService.getScenicCommentInfoById(id);
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.DELETE})
    public ResponseEntity<Integer> deleteCommentById(@RequestParam Integer id) {
        return ResponseEntity.ok(scenicCommentService.deleteScenicCommentInfoById(id));
    }

    @PostMapping("/insert")
    public ResponseEntity<Integer> insertScenicCommentInfo(@RequestBody ScenicCommentInfo scenicCommentInfo) {
        System.out.println("接收到的数据：" + scenicCommentInfo);
        return ResponseEntity.ok(scenicCommentService.insertScenicCommentInfo(scenicCommentInfo)); // 修正变量名
    }

    @PutMapping("/update")
    public ResponseEntity<Integer> updateScenicCommentInfo(@RequestBody ScenicCommentInfo scenicCommentInfo) {
        System.out.println("更新数据：" + scenicCommentInfo);
        return ResponseEntity.ok(scenicCommentService.updateScenicCommentInfo(scenicCommentInfo));
    }

    @GetMapping("/page")
    public PageResult<ViewScenicComment> getScenicCommentInfoByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        PageRequest pageRequest = new PageRequest();
        pageRequest.setPageNum(pageNum);
        pageRequest.setPageSize(pageSize);

        return scenicCommentService.getScenicCommentInfoByPage(pageRequest);
    }

    // 修复此方法，使用正确的返回类型
    @GetMapping("/search")
    public ViewScenicComment getViewScenicCommentById(@RequestParam Integer id) {
        return scenicCommentService.getViewScenicCommentById(id);
    }
}